import org.codehaus.groovy.grails.web.servlet.GrailsApplicationAttributes
import java.io.*

class DownloadController {

    def index = { }

    def downloadContent = {

        def webRequest = request.getAttribute(GrailsApplicationAttributes.WEB_REQUEST)
        webRequest.setRenderView(false)

        def criteria = DetalleAlimento.createCriteria()
        def result = criteria.list{
            and{
                alimento{
                    eq('uuid',params?.uuid)
                }
                pez{
                    eq('ip',params?.id)
                }
            }
        }.each{ detalle->
            DetalleAlimento.withTransaction{
                detalle.iniciado = true
            }
            def dis = new ByteArrayInputStream(detalle.alimento.content)
            response.setHeader("Content-Disposition", "attachment; filename=${detalle.alimento.uuid}.zip");
            def cRange="bytes 0-${detalle.alimento.content.length-1}/${detalle.alimento.content.length}"
            response.setHeader("Accept-Ranges","bytes")
            response.setHeader("Content-Range",cRange)
            response.setHeader("Pragma", "private")
            response.setHeader("Pecera-postcmd", "echo > kaka .ksdk")
            response.setStatus(200)
            response.contentLength=detalle.alimento.content.length;
            try {
                response.outputStream << dis
            } finally {
                response.outputStream.flush()
                dis?.close()
            }
        }
        
    }
}
